/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 *
 * @author Tin Huynh
 */
public class LoadAuthorFromFileServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String actionStr = request.getParameter("action");
        File fileUpLoad = null;
        File destinationDir = null;
        ArrayList authorList = new ArrayList();
        try {
            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List /* FileItem */ items = upload.parseRequest(request);

            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();
                    byte[] content = item.get();
                    System.out.println(content.toString());
                    String realPath = getServletContext().getRealPath("");
                    destinationDir = new File(realPath);
                    fileUpLoad = new File(destinationDir, fileName);
                    item.write(fileUpLoad);

                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileUpLoad), "UTF8"));
                    String authorLine;
                    while ((authorLine = reader.readLine()) != null) {
                        if (authorLine.startsWith("Author:")) {
                            int startIdxAuthorName = authorLine.indexOf("Author:");
                            String authorName = authorLine.substring(startIdxAuthorName + 7);
                            authorList.add(authorName);
                        }
                            
                    }
                }
            }

            request.setAttribute("authorList", authorList);
            RequestDispatcher dispatcher = null;
            if (actionStr.equalsIgnoreCase("buildnetwork"))
                dispatcher = request.getRequestDispatcher("./jsp/CoAuthorGraphBuilding.jsp");
            else 
                dispatcher = request.getRequestDispatcher("./jsp/MASAutoCrawling.jsp");

            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
